﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using Microsoft.Win32;
using Maticsoft.Common;

namespace WEB.compress
{
    public partial class CompressRead : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (null != Request.QueryString["filename"])
            {
                string fileName = Request.QueryString["filename"];
                fileName = Server.UrlDecode(fileName.ToString());
                Session["fileName"] = fileName;   //保存为全局变量
                FileName.Text = fileName.Substring(fileName.LastIndexOf(@"\") + 1);
            }

        }

        protected void btnDown_Click(object sender, EventArgs e)
        {
            string fileName = Session["fileName"].ToString();
            DownloadFile(fileName);
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            string fileName = Session["fileName"].ToString();
            System.IO.FileInfo file = new System.IO.FileInfo(fileName); //原文件
            if (file.Exists)
            {
                file.Delete();
                MessageBox.ShowAndRedirect(this, "删除成功!", "CompressView.aspx");
            }
            else
            {
                MessageBox.Show(this, "删除失败，该文件不存在!");
            }            
        }

        //// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="filename">文件物理地址</param>
        protected void DownloadFile(string filename)
        {
            int intStart = filename.LastIndexOf(@"\") + 1;
            string saveFileName = filename.Substring(intStart, filename.Length - intStart);
            System.IO.FileInfo fi = new System.IO.FileInfo(filename);
            string fileextname = fi.Extension;
            string DEFAULT_CONTENT_TYPE = "application/unknown";
            RegistryKey regkey, fileextkey;
            string filecontenttype;
            try
            {
                regkey = Registry.ClassesRoot;
                fileextkey = regkey.OpenSubKey(fileextname);
                filecontenttype = fileextkey.GetValue("Content Type", DEFAULT_CONTENT_TYPE).ToString();
            }
            catch
            {
                filecontenttype = DEFAULT_CONTENT_TYPE;
            }
            Response.Clear();
            Response.Charset = "gb2312";
            Response.Buffer = true;
            this.EnableViewState = false;
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.AppendHeader("Content-Disposition", "attachment;filename=\"" + Server.UrlEncode(saveFileName) + "\"");

            Response.ContentType = filecontenttype;
            
            Response.WriteFile(filename);
        }
    }
}